package cn.pn.project.service;

import cn.pn.project.common.RedisUtils;
import cn.pn.project.mapper.schedule.TicketFileDao;
import cn.pn.project.pojo.schedule.Ticketfile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.util.List;

@RestController
public class TicketFileService {

    @Autowired
    private TicketFileDao ticketFileDao;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 根据场地id查询票档信息
     * @param placeId
     * @return
     * @throws SQLException
     */
    @RequestMapping(value = "/getTickFileByPlaceId",method = RequestMethod.POST)
    public List<Ticketfile> getTickFileByPlaceId(@RequestParam("placeId") Integer placeId) throws SQLException{
        String key="ticketfile"+placeId;
        if(redisUtils.exist(key)){
            System.out.println("从redis中获取票档信息");
            return (List<Ticketfile>)redisUtils.get(key);
        }else {
            if (redisUtils.set(key, ticketFileDao.getTickFileByPlaceId(placeId))) {
                System.out.println("将票档信息保存到redis中");
            }
            return ticketFileDao.getTickFileByPlaceId(placeId);
        }

    }
}
